package cn.kuick.deal.alert.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 用于提取sql文件的sql语句。
 能过滤掉/* */和--的注释方式。
 每一条sql语句以;结尾，作为一条语句
 */
public class LoadFileExtractSqlSentenceUtil {
    private static final Log log = LogFactory.getLog(LoadFileExtractSqlSentenceUtil.class);

    private static FileInputStream fileInputStream = null;
    private static InputStreamReader inputStreamReader;
    private static BufferedReader bufferedReader;
    private static List sqlList = new ArrayList();

    public static List getSqlSentenceList(String filePath){
        if(filePath.toLowerCase().endsWith(".sql") == false){
            log.info("不是sql文件!");
            return null;
        }

        File file = new File(filePath);

        try {
            fileInputStream = new FileInputStream(file);
            inputStreamReader = new InputStreamReader(fileInputStream);
            bufferedReader = new BufferedReader(inputStreamReader);

            String lineStr = null;

            while (true){
                lineStr = bufferedReader.readLine();

                if(lineStr == null){
                    break;
                }

                if(lineStr.length() < 2){
                    continue;
                }

                if(lineStr.charAt(0) == '/' && lineStr.charAt(1) == '*'){
                    if(!lineStr.endsWith("*/")){
                        if(!filterAnnotationLine()){
                            return null;
                        }
                    }
                    continue;
                }

                if(lineStr.charAt(0) == '-' && lineStr.charAt(1)=='-'){
                    continue;
                }

                if(!addOneSqlSentence(lineStr)){
                    return null;
                }
            }
        } catch (FileNotFoundException e) {
            log.warn("读取文件流失败！" + e);
            return null;
        } catch (IOException e) {
            log.error("缓存读取文件内容失败!" + e);
        }

        return sqlList;
    }

    private static boolean filterAnnotationLine(){
        try {
            while (true){
                String lineStr = bufferedReader.readLine();

                if(lineStr == null){
                    log.info("sql文件注释语法错误。");
                    return false;
                }

                if (lineStr.length() < 2){
                    continue;
                }

                if(lineStr.endsWith("*/")){
                    return true;
                }
            }
        } catch (IOException e) {
            log.error("缓存读取文件内容失败!" + e);
            return false;
        }
    }

    private static Boolean addOneSqlSentence(String lineStr){
        String sqlStr = "";

        try {
            while (true){
                sqlStr = sqlStr + lineStr;

                if(lineStr.endsWith(";")){
                    sqlList.add(sqlStr);
                    return true;
                }

                lineStr = bufferedReader.readLine();

                if(lineStr == null){
                    log.info("sql文件sql语句语法错误。");
                    return false;
                }
            }
        } catch (IOException e) {
            log.error("缓存读取文件内容失败!" + e);
            return false;
        }
    }
}






















